package _1_flyweight

import (
	"math/rand"
	"time"
)

//Suits 牌的花色
type Suits int

const (
	SPADE Suits = iota
	HEART
	DIAMOND
	CLUB
)

//PokerCardUnit 扑克享元
type PokerCardUnit struct {
	Id    int
	Name  string //名称
	Suits Suits  //花色
}

var units = map[int]*PokerCardUnit{
	1: {
		Id:    1,
		Name:  "ACE",
		Suits: SPADE,
	},
	2: {
		Id:    2,
		Name:  "2",
		Suits: SPADE,
	},
	3: {
		Id:    3,
		Name:  "3",
		Suits: SPADE,
	},
	//...忽略其他
	11: {
		Id:    11,
		Name:  "Jack",
		Suits: SPADE,
	},
	12: {
		Id:    12,
		Name:  "Queen",
		Suits: SPADE,
	},
	13: {
		Id:    13,
		Name:  "King",
		Suits: SPADE,
	},
	//...忽略其他颜色
	53: {
		Id:   53,
		Name: "Joker BLACK",
	},
	54: {
		Id:   54,
		Name: "Joker RED",
	},
}

//HandRole 牌规则 角色
type HandRole int

const (
	HandRoleFarmer HandRole = iota + 1
	HandRoleLandlord
)

//PersonPokerCard 牌局角色
type PersonPokerCard struct {
	Person     string
	Role       HandRole
	PokerCards []*PokerCardUnit
}

//PokerHand 牌局
type PokerHand struct {
	PersonPokerCards [3]PersonPokerCard
}

//NewPokerHand 构建牌局
func NewPokerHand(persons [3]string) *PokerHand {
	personPokerCardMap := map[string]PersonPokerCard{
		persons[0]: PersonPokerCard{
			Person:     persons[0],
			Role:       HandRoleFarmer,
			PokerCards: make([]*PokerCardUnit, 17),
		},
		persons[1]: PersonPokerCard{
			Person:     persons[1],
			Role:       HandRoleFarmer,
			PokerCards: make([]*PokerCardUnit, 17),
		},
		persons[2]: PersonPokerCard{
			Person:     persons[2],
			Role:       HandRoleFarmer,
			PokerCards: make([]*PokerCardUnit, 20),
		},
	}
	rand.Seed(time.Now().Unix())
	//打乱牌序，按顺序发给人
	PersonPokerCards := [3]PersonPokerCard{
		personPokerCardMap[persons[0]],
		personPokerCardMap[persons[1]],
		personPokerCardMap[persons[2]],
	}
	return &PokerHand{
		PersonPokerCards: PersonPokerCards,
	}
}
